package com.oocl.jmsbook.consumer.filereader;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.jmsbook.pojo.Book;

public class BookJSONFileReader {

	public static List<Book> readBooksFromFile(String filename) {
		ObjectMapper mapper = new ObjectMapper();
		BufferedReader reader = null;
		List<Book> bookList = new ArrayList<>();
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
			String line;
			while ((line = reader.readLine()) != null) {
				bookList.add(mapper.readValue(line, Book.class));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return bookList;
	}
}
